﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;

namespace CPPEI.ETL.WebApi
{
    /// <summary>
    /// 创建MySql类型的数据源
    /// </summary>
    [CPPEI.WebApi.AuthPostAction("MySql", "CreateDataSource", "1.0")]
    public class CreateDataSourceAction : BaseAction<CreateDataSourceParams, CreateDataSourceResult>
    {
        protected override ActionResult<CreateDataSourceResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            CreateDataSourceParams opParams)
        {
            SER.DtSerService dtSerService = new SER.DtSerService();
            ServiceResult result = dtSerService.CreateDtSource(context.UserId, opParams.DataServiceId, opParams.Name,
                opParams.Value,
                opParams.Type,
                opParams.ParameterSettings, opParams.ColumnSettings,
                null);
            if(result.IsSuccess)
            {
                return new ActionResult<CreateDataSourceResult>(new CreateDataSourceResult());
            }
            else
            {
                return new ActionResult<CreateDataSourceResult>(result.IsSuccess, result.ErrorCode, result.Message, null);
            }
        }
    }

    public class CreateDataSourceParams
    {
        [Parameter]
        public string Name { get; set; }


        [Parameter]
        public Guid DataServiceId { get; set; }

        [Parameter]
        public string Value { get; set; }

        [Parameter]
        public int Type { get; set; }

        /// <summary>
        /// 数据源参数信息，格式：name,type,defaultValue,description,name,type,defaultValue,description
        /// </summary>
        public string ParameterSettings { get; set; }

        [Parameter]
        public string ColumnSettings { get; set; }
    }

    public class CreateDataSourceResult
    {
    }
}
